package com.inspinia.upms.rpc.service;

import com.inspinia.base.common.exception.BusinessException;
import com.inspinia.base.dao.BaseDao;
import com.inspinia.base.pagination.Pager;
import com.inspinia.base.service.BaseServiceImpl;
import com.inspinia.base.util.CollectionUtil;
import com.inspinia.upms.common.model.post.UpmsPost;
import com.inspinia.upms.common.model.post.UpmsPostVo;
import com.inspinia.upms.dao.UpmsPostDao;
import com.inspinia.upms.rpc.api.UpmsDeptService;
import com.inspinia.upms.rpc.api.UpmsPostService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;


/**
 * 岗位的服务层实现类
 *
 * @author veblen
 * @date 2016-9-5
 */
@Service
public class UpmsPostServiceImpl extends BaseServiceImpl<UpmsPost> implements UpmsPostService {
    @Resource
    private UpmsPostDao postDao;
    @Resource
    private UpmsDeptService departmentService;

    @Override
    protected BaseDao<UpmsPost> getDao() {
        return postDao;
    }

    @Override
    public Pager<UpmsPostVo> findPage(UpmsPostVo UpmsPostVo){
        Pager page = super.findPage(UpmsPostVo);
        List<UpmsPostVo> vos = new ArrayList<>(page.getList().size());
        for (UpmsPost post : (List<UpmsPost>) page.getList()) {
            UpmsPostVo vo = new UpmsPostVo();
            BeanUtils.copyProperties(post,vo);
            vo.setDeptName(departmentService.getName(vo.getDeptId()));
            vos.add(vo);
        }
        page.setList(vos);
        return page;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int batchDelete(Long[] ids) {
        batchDeletePostUser(ids);
        batchDeletePostResource(ids);
        return postDao.batchDelete(ids);
    }

    /**
     * 批量删除岗位和资源的关系
     *
     * @param ids 岗位id
     */
    private int batchDeletePostResource(Long[] ids) {
        return postDao.batchDeletePostResource(ids);
    }

    /**
     * 批量删除岗位和用户的关系
     *
     * @param ids 岗位Id集合
     */
    private int batchDeletePostUser(Long[] ids) {
        return postDao.batchDeletePostUser(ids);
    }

    @Override
    public int insert(UpmsPost bean) {
        setOrgId(bean);
        validatePost(bean);
        return super.insert(bean);
    }


    /**
     * 校验岗位信息是否合法
     *
     * @param bean
     */
    private void validatePost(UpmsPost bean) {

        boolean b = validName(bean.getPostName(), bean.getDeptId(), bean.getId());
        if (!b) {
            throw new BusinessException("岗位已存在");
        }
    }

    @Override
    public int update(UpmsPost bean) {
        setOrgId(bean);
        validatePost(bean);
        return super.update(bean);
    }

    /**
     * 设置机构id
     *
     * @param bean 岗位
     */
    private void setOrgId(UpmsPost bean) {
        Long orgId = bean.getOrgId();
        if (orgId != null) {
            orgId = departmentService.getOrgId(bean.getDeptId());
            bean.setOrgId(orgId);
        }
    }

    @Override
    public boolean validName(String name, Long deptId, Long postId) {
        return postDao.countExistedName(name, deptId, postId) == 0;
    }

    @Override
    public UpmsPostVo getVo(Long id){
        UpmsPostVo vo = new UpmsPostVo();
        UpmsPost post = get(id);
        BeanUtils.copyProperties(post,vo);
        vo.setDeptName(departmentService.getName(post.getDeptId()));
        return vo;
    }

    @Override
    public List<UpmsPost> findAll() {
        return postDao.findAll();
    }

    @Override
    public List<String> findNamesByIds(List<Long> postIds) {
        if (CollectionUtil.isNotEmpty(postIds)) {
            return postDao.findNamesByIds(postIds);
        }
        return null;
    }

    @Override
    public List<Long> findResourceIds(Long id) {
        return postDao.findResourceIds(id);
    }

    @Override
    public int setResources(Long postId, List<Long> resourceIds) {
        int i = postDao.deleteResources(postId);
        if (CollectionUtil.isNotEmpty(resourceIds)) {
            return postDao.saveResources(postId, resourceIds);
        }
        return i;

    }

    @Override
    public List<Long> findUserIdsByPostName(String postName) {
        Assert.notNull(postName, "岗位名称不能为空");
        return postDao.findUserIdsByPostName(postName);
    }

    @Override
    public List<Long> findUserIdsByPostId(Long postId) {
        return postDao.findUserIdsByPostId(postId);
    }

    @Override
    public List<UpmsPost> findPostByDeptId(Long deptId){
        return postDao.findPostByDeptId(deptId);
    }

}
